package com.dao;

import com.model.User;
import com.mysql.cj.protocol.Resultset;

import java.sql.*;
import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    //添加用户
    public boolean addUser(User user) {
        String sql = "INSERT INTO users( username, passwd ,id,score)\n" +
                "VALUES\n" +
                "\t( ?, ? ,?,?);";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPasswd());
            preparedStatement.setInt(3, user.getId());
            preparedStatement.setDouble(4, user.getScore());
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    //用于注册合法性检测
    public boolean hasUser(User user) {
        String sql = "SELECT * FROM users WHERE username = ?;";
        String username = user.getUsername();
        String dbUsername = null;
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                dbUsername = resultSet.getString("username");
                if (username.equals(dbUsername)) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    //用于登录验证
    public boolean isPasswdRight(User user) {
        String sql = "SELECT * FROM users WHERE username = ?;";
        String username = user.getUsername();
        String passwd = user.getPasswd();
        String dbPasswd = null;
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                dbPasswd = resultSet.getString("passwd");
                if (dbPasswd.equals(passwd)) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public User getLoginUserByUser(User user) {
        String sql = "Select *\n" +
                "from users\n" +
                "where username=? and passwd=?;";
        User loginUser = new User();
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPasswd());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                loginUser.setUsername(resultSet.getString("username"));
                loginUser.setPasswd(resultSet.getString("passwd"));
                loginUser.setId(resultSet.getInt("id"));
                loginUser.setScore(resultSet.getDouble("score"));
            }
            return loginUser;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return loginUser;
    }

    @Override
    public boolean updateUserInfo(User user) {
        String sql = "update users\n" +
                "set id=?,username=?,score=?\n" +
                "where username=?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, user.getId());
            preparedStatement.setString(2, user.getUsername());
            preparedStatement.setDouble(3, user.getScore());
            preparedStatement.setString(4, user.getUsername());
            preparedStatement.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public ArrayList<User> getAllUser() {
        String sql = "SELECT id,username,score From users";
        ArrayList<User> resultUsers = new ArrayList<User>();
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setScore(resultSet.getDouble("score"));
                resultUsers.add(user);
            }
            return resultUsers;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
